<template>
  <view class="goods_list">
    <!-- 轮播图 -->
    <u-swiper
      :list="goodsDetail.pics"
      name="pics_big"
      height="500"
      img-mode="aspectFit"
      @click="handleDetailImage"
    ></u-swiper>
    <!-- 价格 -->
    <view class="goods_price">￥{{ goodsDetail.goods_price }}</view>
    <!-- 名称 -->
    <view class="goods_name">{{ goodsDetail.goods_name }}</view>
    <!-- 详情 -->
    <u-parse :html="goodsDetail.goods_introduce"></u-parse>
    <!-- 底部提交栏 -->
    <view class="navigation">
      <view class="left">
        <view class="item">
          <u-icon
            name="server-fill"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">客服</view>
        </view>
        <view class="item">
          <u-icon
            name="home"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">店铺</view>
        </view>
        <navigator
          open-type="switchTab"
          url="/pages/cart/cart"
          class="item car"
        >
          <u-badge
            class="car-num"
            :count="totalNums"
            type="error"
            :offset="[-3, -6]"
          ></u-badge>
          <u-icon
            name="shopping-cart"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">购物车</view>
        </navigator>
      </view>
      <view class="right">
        <view class="cart btn u-line-1" @click="handleCardAdd">加入购物车</view>
        <view class="buy btn u-line-1">立即购买</view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";
export default {
  data() {
    return {
      goodsDetail: null,
    };
  },
  computed: {
    ...mapGetters("cart", ["totalNums"]),
  },
  onLoad(option) {
    // console.log(option);  43986
    // 把商品列表的详情id传入
    this.getGoodsDetail(option.goods_id);

    console.log(this.$store);
  },
  methods: {
    ...mapMutations("cart", ["addCart"]),

    // 接收商品列表的详情id
    async getGoodsDetail(goods_id) {
      // 调用接口
      const result = await this.$u.get("/goods/detail", { goods_id });
      console.log(result);
      //   存入data数据中
      this.goodsDetail = result.message;
    },
    handleDetailImage(index) {
      //   console.log(index);
      //   urls需要数组图片，用map过滤出数组里的图片
      const urls = this.goodsDetail.pics.map((v) => v.pics_big);
      //   当前显示图片的链接把urls对应的下标传入current
      const current = urls[index];

      //   previewImage传入2值就可以全屏放大
      uni.previewImage({ urls, current });
    },
    handleCardAdd() {
      //   console.log(1);
      this.addCart(this.goodsDetail);
    },
  },
};
</script>

<style lang="scss" scoped>
.goods_list {
  padding-bottom: 105rpx;
  .goods_price {
    color: red;
    font-size: 35rpx;
    font-weight: 700;
    margin: 25rpx;
  }
  .goods_name {
    font-weight: 700;
    margin: 25rpx;
  }
}
.navigation {
  position: fixed;
  left: 0;
  bottom: 0;

  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>
